package com.yanqu.road.server.cmd.activity.commonteam;

import com.yanqu.road.entity.activity.commonteam.data.CommonTeamUserData;
import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.CommonTeamProto;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.commonteam.CommonTeam;
import com.yanqu.road.server.manager.activity.commonteam.CommonTeamActivity;
import com.yanqu.road.server.manager.activity.commonteam.CommonTeamMgr;
import com.yanqu.road.server.manager.activity.commonteam.pb.CommonTeamPb;
import com.yanqu.road.server.manager.activity.sonhai.pb.SonHaiActivityPb;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.List;

@Cmd(code = Protocol.C2_COMMON_TEAM_GET_RANK_LIST, desc = "通用组队-获取队伍榜单")
public class CrossCommonTeamGetRankListCmd extends CrossUnionCommand {

    private long playerId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        playerId = packet.getPlayerId();
        return true;
    }
    
    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CommonTeamProto.CommonTeamGetRankListReqMsg reqMsg = CommonTeamProto.CommonTeamGetRankListReqMsg.parseFrom(packet.getBytes());
        CommonTeamProto.CommonTeamGetRankListRespMsg.Builder respMsg = CommonTeamProto.CommonTeamGetRankListRespMsg.newBuilder();

        //获取活动
        CommonTeamActivity activity = CommonTeamMgr.getActivity(reqMsg.getActivityId(), groupId);
        if (activity == null) {
            respMsg.setRet(GameErrorCode.E_COMMON_TEAM_TEAM_ACTIVITY_NOT_EXIST);
            MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_COMMON_TEAM_GET_RANK_LIST, respMsg));
            return;
        }
        int conditionType = reqMsg.getConditionType();

        //判断榜单类型是否存在
        boolean isExist = CommonTeamMgr.checkTeamRankConditionType(reqMsg.getActivityId(), conditionType);
        if (!isExist) {
            respMsg.setRet(GameErrorCode.E_COMMON_TEAM_RANK_CONDITION_TYPE_NOT_EXIST);
            MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_COMMON_TEAM_GET_RANK_LIST, respMsg));
            return;
        }

        //获取队伍榜单
        int myTeamId = 0;
        CommonTeamUserData userData = activity.getUserData(packet.getPlayerId());
        if (userData != null) {
            myTeamId = userData.getTeamId();
        }
        long myScore = 0;
        long myParam = 0;
        CommonTeam myTeam = activity.getTeam(myTeamId);
        if (myTeam != null) {
            myScore = myTeam.getRankScore(conditionType);
            myParam = myTeam.getRankScoreParam(conditionType);
        }
        int myRank = activity.getTeamRank(myTeamId, conditionType);
        //榜单LIST
        List<CommonTeam> teamList = activity.getTeamRankList(conditionType);
        respMsg.setRet(0);
        CrossActivityProto.TeamRankListMsg.Builder teamRankListMsg = CrossActivityProto.TeamRankListMsg.newBuilder();
        for (CommonTeam team : teamList) {
            CrossActivityProto.TeamRankTempMsg.Builder teamRankTempMsg = CommonTeamPb.buildTeamRankTempMsg(team, conditionType);
            teamRankListMsg.addRankList(teamRankTempMsg);
        }
        respMsg.setRankList(teamRankListMsg);
        respMsg.setMyRank(myRank);
        respMsg.setMyValue(String.valueOf(myScore));
        respMsg.setMyParam(String.valueOf(myParam));
        //返回
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_COMMON_TEAM_GET_RANK_LIST, respMsg));
    }

    @Override
    public void sendData() {}
}
